#coding: utf-8
import esm
import time
import json
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )

if __name__ == '__main__':
    house_desc = sys.argv[1]
    t1=time.time()
    tagmap = {}
    exclude_basetag_list=['基本属性/建筑类型','交易属性/交易权属']
    all_words = set()
    index = esm.Index()
    count=0
    for line in open('new_tag_content.txt'):
        cols = line.strip().split('\t')
        if len(cols) != 2:
            continue
        tag, words = cols
        words = words.split(' ')
	count=count+1
        for word in words:
            tagmap.setdefault(word, [])
            tagmap[word].append(tag)
            # 添词
            index.enter(word)
    # 添完所有词后，生成查询结构
    index.fix()
    house_desc = sys.argv[1].strip('\n')
    # 匹配词
    mat = set([m[1] for m in index.query(house_desc)])
    mat1=mat
    newmat=set()
    tagset=set()
    exclude_dict={}
    for w in mat:
	subword=0
	for word in mat1:
	    if word!=w and w in word:
	        subword=1
	        break
	if subword==0:
	    if tagmap[w][0] not in tagset:
	        newmat.add(w)
	        tagset.add(tagmap[w][0])
	        tag_base=tagmap[w][0].rsplit('/',1)[0]
	        if tag_base in exclude_basetag_list:
		    if tag_base not in exclude_dict:
		        exclude_dict[tag_base] = 1
		    else:
		        exclude_dict[tag_base] = exclude_dict[tag_base] + 1
	
    for w in newmat:
	tag_base=tagmap[w][0].rsplit('/',1)[0]
	if tag_base not in exclude_dict or exclude_dict[tag_base] ==1:
	    print tagmap[w][0]+":"+w
	    #print tagmap[w]+" "+w

